package com.shiyi.util.toc;

import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfOutline;
import com.itextpdf.kernel.pdf.navigation.PdfDestination;
import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TextRenderer;

public class TOCTextRenderer extends TextRenderer {
 
	protected PdfOutline parentOutline;
 
	public TOCTextRenderer(final PdfOutline parentOutline, final Text modelElement) {
		super(modelElement);
		this.parentOutline = parentOutline;
	}
 
	@Override
	public void draw(final DrawContext drawContext) {
 
		super.draw(drawContext);
 
		Text t = (Text) this.modelElement;
 
		Rectangle rect = this.getOccupiedAreaBBox();
		PdfDestination dest = PdfExplicitDestination.createXYZ(drawContext.getDocument().getLastPage(), rect.getLeft(),
				rect.getTop(), 0);
		PdfOutline curOutline = this.parentOutline.addOutline(t.getText());
		curOutline.addDestination(dest);
 
		// PdfOutline secOutline = curOutline.addOutline(t.getText() + " - 2");
		// secOutline.addDestination(dest);
		// System.out.println(curOutline.getTitle());
	}
 
}